import { onUnmounted, ref, toRefs } from "vue";

/**
 * 获取主应用数据
 */
export default (...args) => {
  const mainData = ref({});
  const handleDataChange = data => {
    if (args && args.length) {
      let dataObj = {};
      args.forEach(item => {
        dataObj[item] = data[item];
      });
      mainData.value = dataObj;
      return;
    }
    mainData.value = data;
  };
  window.microApp && window.microApp.addDataListener(handleDataChange, true);
  onUnmounted(() => {
    window.microApp && window.microApp.removeDataListener(handleDataChange);
  });
  return toRefs(mainData.value);
};
